package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public DishOverViewVO overviewDishes() {
        return DishOverViewVO.builder()
                .discontinued(dishMapper.selectCount(new QueryWrapper<Dish>().eq("status", StatusConstant.DISABLE)).intValue())
                .sold(dishMapper.selectCount(new QueryWrapper<Dish>().eq("status", StatusConstant.ENABLE)).intValue())
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        return OrderOverViewVO.builder()
                .allOrders(orderMapper.selectCount(null).intValue())
                .cancelledOrders(getOrderCount(Orders.CANCELLED))
                .completedOrders(getOrderCount(Orders.COMPLETED))
                .deliveredOrders(getOrderCount(Orders.CONFIRMED) + getOrderCount(Orders.DELIVERY_IN_PROGRESS))
                .waitingOrders(getOrderCount(Orders.TO_BE_CONFIRMED))
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        return SetmealOverViewVO.builder()
                .discontinued(setmealMapper.selectCount(new QueryWrapper<Setmeal>().eq("status", StatusConstant.DISABLE)).intValue())
                .sold(setmealMapper.selectCount(new QueryWrapper<Setmeal>().eq("status", StatusConstant.ENABLE)).intValue())
                .build();
    }

    @Override
    public BusinessDataVO businessData() {
        Integer newUsers = userMapper.selectCount(new QueryWrapper<User>().ge("create_time", LocalDate.now().atStartOfDay())).intValue();
        List<Orders> ordersList = orderMapper.selectList(new QueryWrapper<Orders>().ge("checkout_time", LocalDate.now().atStartOfDay()));
        double turnover = 0.0;
        for (Orders orders : ordersList) {
            turnover = turnover +  orders.getAmount().doubleValue();
        }
        Integer validOrderCount = getOrderCount(Orders.COMPLETED);
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .turnover(turnover)
                .unitPrice(ordersList.isEmpty() ? 0.0 : turnover / ordersList.size())
                .validOrderCount(validOrderCount)
                .orderCompletionRate(ordersList.isEmpty() ? 0.0 : 1.0 * validOrderCount / ordersList.size())
                .build();
    }

    private Integer getOrderCount(Integer status) {
        return orderMapper.selectCount(new QueryWrapper<Orders>().eq("status", status)).intValue();
    }
}
